import numpy as np
import matplotlib.pyplot as plt
from rk45 import cal_temperature
import time
from data_record import data_record
R_1 = 0.0012
R_2 = 0.0093
C_in = 1100000
C_wall = 186000000
theta_out = -25
dt = 10


cal_temp = []
dr = []
# init
for i in range(600):
    cal_temp.append(cal_temperature(dt, R_1, R_2, C_in, C_wall))
    dr.append(data_record(i,'test'))
    if(i%2 == 0):
        cal_temp[i].reset_B_P()
    else:
        cal_temp[i].reset_B_0()

    cal_temp[i].reset_out_tempurature(theta_out)
    cal_temp[i].set_start_theta(np.random.uniform(18, 22), 14.808)


# cal
first_last_time = time.time()
for i in range(600):
    last_time = time.time()
    print("cal %ith"%i)
    for index in range(int(86400/dt)):
        cal_temp[i].rk45()
        if (cal_temp[i].get_theta()[0,0] < 18):
            cal_temp[i].reset_B_P()
        #     print("less than 18,now temp %d"%cal_temp.get_theta()[0,0])
        
        elif (cal_temp[i].get_theta()[0,0] > 22):
            cal_temp[i].reset_B_0()
        #     print("over 22,now temp %d"%cal_temp.get_theta()[0,0])
        dr[i].data_append(index,cal_temp[i].get_theta()[0,0],cal_temp[i].get_theta()[1,0],cal_temp[i].P_status)
    print("index {}cost time {}s".format(i,time.time() - last_time))

print("cost time {}s".format(time.time() - first_last_time))


# read data and plot
plt.subplot(1, 2, 1)
Power_all = np.zeros((int(86400/dt)))

for i in range(600):
    # dr[i].data_dump()

    plot_data = dr[i].data_get()
    x_axis = plot_data[:,0]/360
    temp_in = plot_data[:,1]
    temp_wall = plot_data[:,2]
    P_status = plot_data[:,3]
    Power_all = Power_all + P_status
    # plt.plot(x_axis,temp_in)
    plt.plot(x_axis,P_status)
plt.subplot(1, 2, 2)
plt.plot(x_axis,Power_all*8000)
print("avg power {} kw".format(np.sum((Power_all*8))/int(86400/dt)))

plt.show()


